/**
 * 菜单
 * */
import { cloneDeep } from 'lodash';
import { includeArray } from '@/libs/system';
import { getRoutersData } from '@/api/account'
import { backendMenusToRouters } from '@/libs/util.router'

// 根据 menu 配置的权限，过滤菜单
function filterMenu (menuList, access, lastList) {
    menuList.forEach(menu => {
        let menuAccess = menu.auth;

        if (!menuAccess || includeArray(menuAccess, access)) {
            let newMenu = {};
            for (let i in menu) {
                if (i !== 'children') newMenu[i] = cloneDeep(menu[i]);
            }
            if (menu.children && menu.children.length) newMenu.children = [];

            lastList.push(newMenu);
            menu.children && filterMenu(menu.children, access, newMenu.children);
        }
    });
    return lastList;
}

export default {
    namespaced: true,
    state: {
        // 顶部菜单
        header: [],
        // 侧栏菜单
        sider: [],
        // 当前顶栏菜单的 name
        headerName: '',
        // 当前所在菜单的 path
        activePath: '',
        // 展开的子菜单 name 集合
        openNames: [],
        // 2.3.0 所有的菜单
        menuSider: [],
        routers: [], // 路由数据表
        hasGetRouter: false // 是否已经拿过路由数据
    },
    getters: {
        /**
         * @description 根据 user 里登录用户权限，对侧边菜单进行鉴权过滤
         * */
        filterSider (state, getters, rootState) {
            const userInfo = rootState.admin.user.info;
            // @权限
            const access = userInfo.access;
            if (access && access.length) {
                return filterMenu(state.sider, access, []);
            } else {
                return filterMenu(state.sider, [], []);
            }
        },
        /**
         * @description 根据 user 里登录用户权限，对顶栏菜单进行鉴权过滤
         * */
        filterHeader (state, getters, rootState) {
            const userInfo = rootState.admin.user.info;
            // @权限
            const access = userInfo.access;
            if (access && access.length) {
                return state.header.filter(item => {
                    let state = true;
                    if (item.auth && !includeArray(item.auth, access)) state = false;

                    return state;
                });
            } else {
                return state.header.filter(item => {
                    let state = true;
                    if (item.auth && item.auth.length) state = false;
                    return state;
                });
            }
        },
        /**
         * @description 当前 header 的全部信息
         * */
        currentHeader (state) {
            return state.header.find(item => item.name === state.headerName);
        },
        /**
         * @description 在当前 header 下，是否隐藏 sider（及折叠按钮）
         * */
        hideSider (state, getters) {
            let visible = false;
            if (getters.currentHeader && 'hideSider' in getters.currentHeader) visible = getters.currentHeader.hideSider;
            return visible;
        }
    },
    mutations: {
        /**
         * @description 设置侧边栏菜单
         * @param {Object} state vuex state
         * @param {Array} menu menu
         */
        setSider (state, menu) {
            state.sider = menu;
        },
        /**
         * @description 设置顶栏菜单
         * @param {Object} state vuex state
         * @param {Array} menu menu
         */
        setHeader (state, menu) {
            state.header = menu;
        },
        /**
         * @description 设置当前顶栏菜单 name
         * @param {Object} state vuex state
         * @param {Array} name headerName
         */
        setHeaderName (state, name) {
            state.headerName = name;
        },
        /**
         * @description 设置当前所在菜单的 path，用于侧栏菜单高亮当前项
         * @param {Object} state vuex state
         * @param {Array} path fullPath
         */
        setActivePath (state, path) {
            state.activePath = path;
        },
        /**
         * @description 设置当前所在菜单的全部展开父菜单的 names 集合
         * @param {Object} state vuex state
         * @param {Array} names openNames
         */
        setOpenNames (state, names) {
            state.openNames = names;
        },
        /**
         * @description 2.3.0，设置所有菜单
         * @param {Object} state vuex state
         * @param {Array} menuSider menuSider
         * */
        setMenuSider (state, menuSider) {
            state.menuSider = menuSider;
        },
        /* 设置路由数据 */
        setRouters(state, routers) {
            state.routers = routers
        },
        /* 是否已经拿过路由 */
        setHasGetRouter(state, status) {
            state.hasGetRouter = status
        }
    },
    actions: {
        // 获取路由数据
        getRouters({commit}) {
            return new Promise((resolve, reject) => {
                try {
                    getRoutersData().then(res=> {
                        // let routers = backendMenusToRouters(res.data.data.routersData)
                        // let routers = JSON.parse(JSON.stringify(routersData))
                        console.log('getRoutersData触发了')
                        let routers = routersData
                        commit('setRouters', routers)
                        commit('setHasGetRouter', true)
                        resolve(routers)
                    }).catch(err=> {
                        reject(err)
                    })
                } catch (error) {
                    reject(errot)
                }
            })
        }
    }
}

const routersData = [
    {
        path: '/',
        component: () => import('@/layouts/basic-layout'),
        children: [
            {
                path: 'tabs/examinee',
                name: 'examinee',
                meta: {
                    title: '管理员管理',
                    auth: ['admin'],
                    closable: false
                },
                component: () => import('@/pages/tabs/examinee/index')
            },
            {
                path: 'tabs/examineeInfo',
                name: 'examineeInfo',
                meta: {
                    title: '考生信息',
                    auth: ['admin'],
                    closable: false
                },
                component: () => import('@/pages/tabs/examinee/component/examineeInfo')
            },
            {
                path: 'tabs/examiner',
                name: 'examiner',
                meta: {
                    title: '考务人员管理',
                    auth: ['admin'],
                    closable: false
                },
                component: () => import('@/pages/tabs/examiner/index')
            },
            {
                path: 'tabs/examinerInfo',
                name: 'examinerInfo',
                meta: {
                    title: '考务人员信息',
                    auth: ['admin'],
                    closable: false
                },
                component: () => import('@/pages/tabs/examiner/component/examinerInfo')
            },
            {
                path: 'tabs/',
                redirect: {
                    name: 'project'
                },
                component: ()=> import('@/pages/tabs/common/viewMiddleware'),
                children: [
                    {
                        path: 'project',
                        name: 'project',
                        meta: {
                            title: '考务项目管理',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/index')
                    },
                    {
                        path: 'project/setnotice',
                        name: 'setnotice',
                        meta: {
                            title: '公告设置',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/setnotice')
                    },
                    {
                        path: 'project/setProjectFlow',
                        name: 'setProjectFlow',
                        meta: {
                            title: '项目流程设置',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/setProjectFlow')
                    },
                    {
                        path: 'project/setstation',
                        name: 'setstation',
                        meta: {
                            title: '岗位设置',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/setstation')
                    },
                    {
                        path: 'project/applyCheck',
                        name: 'applyCheck',
                        meta: {
                            title: '报名审查',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/applyCheck')
                    },
                    {
                        path: 'project/admissionTicket',
                        name: 'admissionTicket',
                        meta: {
                            title: '准考证',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/admissionTicket')
                    },
                    {
                        path: 'project/resultPublish',
                        name: 'resultPublish',
                        meta: {
                            title: '成绩公布',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/resultPublish')
                    },
                    {
                        path: 'project/interviewSign',
                        name: 'interviewSign',
                        meta: {
                            title: '面试签到',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/interviewSign')
                    },
                    {
                        path: 'project/Q_Asession',
                        name: 'Q_Asession',
                        meta: {
                            title: '考生问答',
                            auth: ['admin'],
                            closable: false
                        },
                        component: () => import('@/pages/tabs/examinationProject/project/Q_Asession')
                    }
                ]
            }
        ]
    }
]